﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Logistics.Contract
{
    public class CurrencyContractRepository : ICurrencyContractRepository
    {
        private readonly LogisticsDbContext db;
        public CurrencyContractRepository(LogisticsDbContext dbc)
        {
            db = dbc;
        }
        public async Task<int> Create(Currency cu)
        {
            await db.Currency.AddAsync(cu);
            return db.SaveChanges();
        }

        public async Task<int> Delete(int Id)
        {
            var currency = await db.Currency.FindAsync(Id);
            db.Currency.Remove(currency);
            return db.SaveChanges();
        }

        public async Task<Currency> GetCurrencyById(int Id)
        {
            var ca = await db.Currency.FindAsync(Id);
            return ca;
        }

        public Task<List<Currency>> GetCurrencys()
        {
            var carr = db.Currency.ToList();
            var file = db.Field.ToList();
            var lin = db.Line.ToList();
            var own = db.Owner.ToList();
            var list = from a in carr
                       join b in own on a.OtherPartyPerson equals b.Id
                       join c in file on a.OppositeUnit equals c.Id
                       join d in lin on a.Line equals d.Id
                       select new
                       {
                           a.Id,
                           a.NumberId,
                           a.ContractTitle,
                           a.OppositeUnit,
                           a.OtherPartyPerson,
                           a.Line,
                           a.Freightrate,
                           a.CharterConditions,
                           a.CharterAmount,
                           a.SigningDate,
                           a.Handler,
                           a.ProjectDescription,
                           a.ChangeClause,
                           a.Enclosure,
                           a.CreationTime,
                           OName = b.Name,
                           FName = c.Name,
                           LName = d.Name,
                       };
            return (Task<List<Currency>>)list;
        }

        public async Task<int> Update(Currency cu)
        {
            var list = await db.Currency.FindAsync(cu.Id);
            list = cu;
            db.Entry(list).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return db.SaveChanges();
        }
    }
}
